layui.use(['layer','table','form','laydate','element'], function(args){
    layer = layui.layer;
    form = layui.form;
    laydate = layui.laydate;
    element = layui.element;
    form.on('submit(process-properties-from)', function(data){
        var loadIndex = layer.load(2);
        $.ajax({
            url: 'defintion/startByFormData.do',
            data: {properties: JSON.stringify(data.field),id:data.field.id},
            dataType: 'json',
            type:'post',
            success: function (data, textStatus) {
                layer.close(loadIndex);
                layer.alert(data.message,function(index){
                    if (data.success) {
                        location.reload();
                    }
                    layer.close(index);
                });
            }
        });
        return false;
    });
});

/**
 * 显示添加窗口
 */
function showAddWindow() {
    layer.open({
        type: 1,
        title: "新增", //不显示标题
        content: $('#addForm'), //捕获的元素，注意：最好该指定的元素要存放在body最外层，否则可能被其它的相对元素所影响
        btn: ['提交','取消'],
        area:['600px','500px'],
        yes: function(index, layero){
            var loadIndex = layer.load(2);
            var formData = new FormData(document.getElementById("addForm"));//表单id
            $.ajax({
                processData:false,
                contentType:false,
                type: 'POST',
                url: 'defintion/add.do',
                data: formData,
                dataType: 'json',
                success: function (data, textStatus) {
                    layer.close(loadIndex);
                    layer.alert(data.message,function (index) {
                        if (data.success) {
                            location.reload();
                        }
                        layer.close(index);
                    })
                }
            })
        }
    });
}

/**
 * 显示修改窗口
 * @param id 流程定义ID
 */
function showUpdateWindow(id) {
    var loadIndex = layer.load(2);
    //获取流程定的XML内容
    $.ajax({
        type: 'get',
        url:'defintion/getXml.do?processDefinitionId='+id,
        dataType: 'json',
        success: function (data, textStatus) {
            if (!data.success)
                layer.alert(data.message);
            else{
                $('#updateForm input').val(id);
                $('#updateForm textarea').val(data.data);
            }
            layer.close(loadIndex);
        }
    });
    //打开窗口
    layer.open({
        type: 1,
        maxmin: true,
        title: "悠",
        content: $('#updateForm'), //捕获的元素，注意：最好该指定的元素要存放在body最外层，否则可能被其它的相对元素所影响
        btn: ['提交','取消'],
        area:['600px','500px'],
        yes: function(index, layero){
            var loadIndex2 = layer.load(2);
            $.ajax({
                type: 'POST',
                url:'defintion/updateXml.do',
                data: $("#updateForm").serialize(),
                dataType: 'json',
                success: function (data, textStatus) {
                    layer.close(loadIndex2);
                    layer.alert(data.message,function (index) {
                        layer.close(index);
                        if (data.success) {
                            location.reload();
                        }
                    })
                }
            });
        }
    });
}

/**
 * 显示流程图
 * @param id 流程定义ID
 */
function showGraph(id) {
    layer.open({
        title:false,
        closeBtn: 0,
        shadeClose: true,
        offset: '100px',
        type: 1,
        area:'800px',
        skin: 'layui-layer-rim',
        content: '<img width="700px" src="defintion/graph.htm?processDefinitionId='+id+'"/>'
    });
}

/**
 * 显示流程启动表单窗口
 * @param id
 */
function showFromWindow(id) {
    var loadIndex = layer.load(2);
    $.ajax({
        url: basePath+'process/form/getStartFromHtml.do',
        data: {processDefintionId: id},
        dataType: 'text',
        success: function (data, textStatus) {
            layer.close(loadIndex);
            layer.open({
                title:"填写流程表单",
                type: 1,
                area:['500px'],
                content: '<form id="processPropertiesForm" action=""  class="layui-form" style="padding: 10px;"><input type="hidden" name="id" value="'+id+'"/>'+data+'<div class="layui-form-item">' +
                '        <div class="layui-input-block">' +
                '            <button class="layui-btn" lay-submit lay-filter="process-properties-from">提交</button>' +
                '            <button type="reset" class="layui-btn layui-btn-primary">重置</button>' +
                '        </div>' +
                '    </div></form>'
            });
            form.render('select');
            laydate.render({
                elem:'#processPropertiesForm input.date'
                ,type:'datetime'
                ,format: 'yyyy-MM-dd HH:mm'
                ,min: '2017-8-11 08:30:00'
                ,max: '2017-12-30 17:30:00'
            })
        }
    });


}
function del(deploymentId,cascade) {
    if (deploymentId) {
        var loadIndex = layer.load(2);
        $.ajax({
            url: 'defintion/deleteByDeploymentId.do',
            data: {deploymentId: deploymentId,cascade:cascade},
            dataType: 'json',
            success: function (data, textStatus) {
                layer.close(loadIndex);
                layer.alert(data.message,function (index) {
                    layer.close(index);
                    if (data.success) {
                        location.reload();
                    }
                });
            }
        });
    } else {
        layer.alert("deploymentId为空")
    }
}
/**
 * 发起流程
 * @param key
 * @param hasStartFormKey
 */
function start(key,id) {
    var loadIndex1 = layer.load(2);
    $.ajax({
        url: basePath+'process/form/getStartFromHtml.do',
        data: {processDefintionId: id},
        dataType: 'text',
        success: function (data, textStatus) {
            layer.close(loadIndex1);
            if (data && data!='' && data.length!=0) {//有表单
                layer.open({
                    title:"填写流程表单",
                    type: 1,
                    area:['500px'],
                    content: '<form id="processPropertiesForm" action=""  class="layui-form" style="padding: 10px;"><input type="hidden" name="id" value="'+id+'"/>'+data+'<div class="layui-form-item">' +
                    '        <div class="layui-input-block">' +
                    '            <button class="layui-btn" lay-submit lay-filter="process-properties-from">提交</button>' +
                    '            <button type="reset" class="layui-btn layui-btn-primary">重置</button>' +
                    '        </div>' +
                    '    </div></form>'
                });
                form.render('select');
                form.render('radio');
                laydate.render({
                    elem:'#processPropertiesForm input.date'
                    ,type:'datetime'
                    ,format: 'yyyy-MM-dd HH:mm'
                    ,min: '2017-8-11 08:30:00'
                    ,max: '2017-12-30 17:30:00'
                });
            }else {
                var loadIndex = layer.load(2);
                $.ajax({
                    url: 'defintion/start.do',
                    data: {key: key},
                    dataType: 'json',
                    success: function (data, textStatus) {
                        layer.close(loadIndex);
                        layer.alert(data.message,function(index){
                            if (data.success) {
                                location.reload();
                            }
                            layer.close(index);
                        });
                    }
                });
            }
        }
    });
}

/**
 *挂起或激活流程
 * @param id 流程定义ID
 * @param suspendOrActivate 挂起还是激活，suspend挂起，activate激活
 * @return
 */
function suspendOrActivate(id,suspendOrActivate) {
    var loadIndex = layer.load(2);
    $.ajax({
        url: 'defintion/suspendOrActivate.do',
        data: {processDefinitionId: id,suspendOrActivate:suspendOrActivate},
        type:'get',
        dataType: 'json',
        success: function (data, textStatus) {
            layer.close(loadIndex);
            layer.alert(data.message,function (index) {
                layer.close(index);
                if (data.success) {
                    location.reload();
                }
            });
        }
    });
}